Binary Tree 发表于 2018-12-06 | 更新于: 2019-02-16 | 分类于 Data Structure , Binary Tree | | 阅读数 字数统计: 1.4k | 阅读时长 ≈ 7 二叉树的定义、实现、遍历、输出、应用。 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306#include <iostream>#include <stack>#include <queue>using namespace std;struct BiTNode { int data; BiTNode *lchild; BiTNode *rchild;};class BinaryTree {private: BiTNode *bt; int RCreate(BiTNode *p, int k, int end);public: void CreateBiTree(int end); BinaryTree() { bt = NULL; } void Release(BiTNode *root); ~BinaryTree(); int PreTraverse(BiTNode *p); int InTraverse(BiTNode *p); int PostTraverse(BiTNode *p); void PreOrderTraverse(); void InOrderTraverse(); void PostOrderTraverse(); void LevelOrderTraverse(); void LeafCount(); void BiTreeDepth(); BiTNode *GetTreeNode(int item, BiTNode *lptr, BiTNode *rptr); BiTNode *CopyTree(BiTNode *p); int CompareTree(BiTNode *t1, BiTNode *t2); BiTNode *GetRoot(); void BiTreeDisplay(BiTNode *bt, int level = 1);};//创建二叉树递归函数int BinaryTree::RCreate(BiTNode * p, int k, int end) { BiTNode *q; int e; cin >> e; if (e != end) { q = new BiTNode; q->data = e; q->lchild = NULL; q->rchild = NULL; if (k == 1) p->lchild = q; if (k == 2) p->rchild = q; RCreate(q, 1, end); RCreate(q, 2, end); } return 0;}//按先序序列创建二叉树void BinaryTree::CreateBiTree(int end) { cout << "请按先序序列的顺序输入二叉树,0为空指针域标志:" << endl; BiTNode *p; int e; cin >> e; if (e == end) return; p = new BiTNode; if (!p) { cout << "申请内存失败!" << endl; exit(-1); } p->data = e; p->lchild = NULL; p->rchild = NULL; bt = p; RCreate(p, 1, end); RCreate(p, 2, end);}//先序遍历递归函数int BinaryTree::PreTraverse(BiTNode *p) { if (p != NULL) { cout << p->data << ' '; PreTraverse(p->lchild); PreTraverse(p->rchild); } return 0;}//中序遍历递归函数int BinaryTree::InTraverse(BiTNode *p) { if (p != NULL) { InTraverse(p->lchild); cout << p->data << ' '; InTraverse(p->rchild); } return 0;}//后续遍历递归函数int BinaryTree::PostTraverse(BiTNode * p) { if (p != NULL) { PostTraverse(p->lchild); PostTraverse(p->rchild); cout << p->data << ' '; } return 0;}//销毁二叉树递归函数void BinaryTree::Release(BiTNode *root) { if (root != NULL) { Release(root->lchild); Release(root->rchild); delete root; }}//析构函数BinaryTree::~BinaryTree() { Release(bt);}//非递归先序遍历二叉树void BinaryTree::PreOrderTraverse() { cout << "先序(非递归)遍历二叉树:"; BiTNode *p = bt; stack<BiTNode> s; while (p || !s.empty()) { if (p) { cout << p->data << ' '; s.push(*p); p = p->lchild; } else { p = new BiTNode; *p = s.top(); s.pop(); p = p->rchild; } } cout << endl;}//非递归中序遍历二叉树void BinaryTree::InOrderTraverse() { cout << "中序(非递归)遍历二叉树:"; BiTNode *p = bt; stack<BiTNode> s; while (p || !s.empty()) { if (p) { s.push(*p); p = p->lchild; } else { p = new BiTNode; *p = s.top(); s.pop(); cout << p->data << ' '; p = p->rchild; } } cout << endl;}//非递归后序遍历结点类型struct BiTNode1 { BiTNode *bt; int tag;};//非递归后序遍历二叉树void BinaryTree::PostOrderTraverse() { cout << "后序(非递归)遍历二叉树:"; BiTNode *p = bt; stack<BiTNode1> s; BiTNode1 *temp; while (p || !s.empty()) { if (p) { BiTNode1 *t = new BiTNode1; t->bt = p; t->tag = 1; s.push(*t); p = p->lchild; } else { temp = new BiTNode1; *temp = s.top(); s.pop(); if (temp->tag == 1) { temp->tag = 2; s.push(*temp); p = new BiTNode; p = temp->bt->rchild; } else { cout << temp->bt->data << ' '; p = NULL; } } } cout << endl;}//层次遍历二叉树void BinaryTree::LevelOrderTraverse() { cout << "层次遍历二叉树:"; queue<BiTNode> q; BiTNode *t; if (bt) { q.push(*bt); while (!q.empty()) { t = new BiTNode; *t = q.front(); q.pop(); if (t) cout << t->data << ' '; if (t->lchild) q.push(*t->lchild); if (t->rchild) q.push(*t->rchild); } } cout << endl;}//计算叶子的个数递归函数void Leaf(BiTNode *p, int &count) { if (p) { Leaf(p->lchild, count); Leaf(p->rchild, count); if (p->lchild == NULL && p->rchild == NULL) count++; }}//后序遍历计算叶子的个数void BinaryTree::LeafCount() { int count = 0; Leaf(bt, count); cout << "叶子的个数为" << count << endl;}//计算深度递归函数void Depth(BiTNode *p, int level, int &depth) { if (p->lchild) Depth(p->lchild, level + 1, depth); if (p->rchild) Depth(p->rchild, level + 1, depth); if (level > depth) depth = level;}//后序遍历计算深度void BinaryTree::BiTreeDepth() { int depth = 0; if (bt) { Depth(bt, 1, depth); } cout << "二叉树的深度为" << depth << endl;}//获取二叉树的一个结点BiTNode *BinaryTree::GetTreeNode(int item, BiTNode * lptr, BiTNode * rptr) { BiTNode *p = new BiTNode; p->data = item; p->lchild = lptr; p->rchild = rptr; return p;}//先序遍历复制一棵二叉树BiTNode *BinaryTree::CopyTree(BiTNode *p) { BiTNode *newlptr, *newrptr; if (p == NULL) return NULL; if (p->lchild) newlptr = CopyTree(p->lchild); else newlptr = NULL; if (p->rchild) newrptr = CopyTree(p->rchild); else newrptr = NULL; bt = GetTreeNode(p->data, newlptr, newrptr);}//先序遍历判断两棵二叉树是否相等int BinaryTree::CompareTree(BiTNode * t1, BiTNode * t2) { if (t1 == NULL && t2 == NULL) return 1; else if (t1->data == t2->data&&CompareTree(t1->lchild, t2->lchild) && CompareTree(t1->rchild, t2->rchild)) return 1; else return 0;}//获取根结点BiTNode* BinaryTree::GetRoot() { if (bt != NULL) return bt; return NULL;}//反中序递归横向树状显示一棵二叉树void BinaryTree::BiTreeDisplay(BiTNode * bt, int level) { if (bt) { BiTreeDisplay(bt->rchild, level + 1); for (int i = 0; i < level; i++) cout << " "; cout << bt->data << endl; BiTreeDisplay(bt->lchild, level + 1); }}int main() { BinaryTree bit; BinaryTree bit1; while (1) { bit.CreateBiTree(0); bit.BiTreeDisplay(bit.GetRoot(),0); cout << "先序(递归)遍历二叉树:"; bit.PreTraverse(bit.GetRoot()); cout << endl; cout << "中序(递归)遍历二叉树:"; bit.InTraverse(bit.GetRoot()); cout << endl; cout << "后序(递归)遍历二叉树:"; bit.PostTraverse(bit.GetRoot()); cout << endl; bit.PreOrderTraverse(); bit.InOrderTraverse(); bit.PostOrderTraverse(); bit.LevelOrderTraverse(); bit.LeafCount(); bit.BiTreeDepth(); bit1.CopyTree(bit.GetRoot()); bit1.BiTreeDisplay(bit1.GetRoot(), 0); if (bit1.CompareTree(bit.GetRoot(), bit1.GetRoot())) cout << "相等,拷贝成功" << endl; cout << endl; } return 0;} 本文作者: Einsturing 本文链接: http://einsturing.top/2018/12/06/二叉树(Binary Tree)/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!